package com.magoldeditor.view;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.util.Iterator;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;

import com.magoldeditor.at.MaEditorXMLException;
import com.magoldeditor.controller.MenuMouseListener;
import com.magoldeditor.util.xml.ContentXMLReader;

/**
 * 
 * @author Velconia
 * @version v.2.0
 * 
 */
public class MaEditorMenuButton extends JToggleButton {
	
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 3L;
	
	/**
	 * default color
	 */
	private static final Color GROUND_COLOR = Color.black;

	/**
	 * default Bounds
	 */
	private static final int X_DEF = 0;
	private static final int Y_DEF = 0;
	private static final int WIDTH_DEF = 200;
	private static final int HEIGHT_DEF = 40;
	
	/**
	 * name
	 */
	protected String name = "MaEditorMenuButton";
	
	/**
	 * menu
	 */
	private JPopupMenu menu = null;
	
	/**
	 * 
	 * @param x
	 * @param y
	 */
	public MaEditorMenuButton() {
		this(null, null);
	}
	
	/**
	 * 
	 * @param text
	 */
	public MaEditorMenuButton(final String text) {
		this(null, text);
	}
	
	/**
	 * 
	 * @param itemListener
	 * @param text
	 */
	public MaEditorMenuButton(final MenuMouseListener itemListener, final String text) {
		refresh(itemListener, text);
	}
	
	/**
	 * 
	 * @param itemListener
	 * @param text
	 */
	protected void refresh(final MenuMouseListener itemListener, final String text) {
		//contants
		ViewContants contants = ViewContants.getNewIntance();
				
		//name
		if (text != null) this.name = text;
		
		//config
		try {
			ViewContants.initConfig(this, this.name);
		} catch (MaEditorXMLException e) {
			e.printStackTrace();
			this.setBounds(X_DEF, Y_DEF, WIDTH_DEF, HEIGHT_DEF);
		}
		
		//visible
		this.setVisible(true);
				
		//color
		this.setBackground(GROUND_COLOR);
		this.setForeground(GROUND_COLOR);
		
	    //menu
	    menu = new JPopupMenu();
	    
	    //add item
		Iterator<String> reader;
		try {
			reader = new ContentXMLReader(ViewContants.CONFIG_ASSERT + "entitycontants.xml");
			while (reader.hasNext()) {
				
				String s = reader.next();
				
				if (s != null) {
					JMenuItem item = new MaEditorMenuItem(itemListener.clone());
					item.setText(s);
					menu.add(item);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		//init menu
		final String selectedSign = contants.getComponentItem(name, "selectedSign");
	    final String noSelectedSign = contants.getComponentItem(name, "notSelectedSign");
	    
	    //text
	  	final String txt = contants.getComponentItem(name, ViewContants.TEXT_NAME);
	  	
	    this.setText(noSelectedSign + txt);
	    
	    this.setHorizontalTextPosition(SwingConstants.CENTER);
	    
	    //add menu listener
	    this.addMouseListener(new MouseAdapter() {
	        @Override
	        public void mouseClicked(MouseEvent e) {  
	            if(isSelected()){
	            	menu.setVisible(true);
	                menu.show(MaEditorMenuButton.this, MaEditorMenuButton.this.getWidth(), MaEditorMenuButton.this.getHeight());
	                setText(selectedSign + txt);
	            }else{
	                setText(noSelectedSign + txt);
	                menu.setVisible(false);
	            }
	        }
	     });
	    
	    //refresh the ui
	    this.repaint();
	}
}
